const product = require('../models/product')
// 获取全部商品
const getProducts = async (ctx) => {
  const query = ctx.request.query
  const data = await product.getProducts(query)
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 获取上架商品
const getStatusProducts = async (ctx) => {
  const query = ctx.request.query
  console.log(query)
  const data = await product.getStatusProducts(query)
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 获取热门商品
const getHotProducts = async (ctx) => {
  const data = await product.getHotProducts()
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }

}
// 添加商品
const addProduct = async (ctx) => {
  const data = ctx.request.body
  const result = await product.addProduct(data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据id获取商品
const getProductById = async (ctx) => {
  const id = ctx.params.id
  const data = await product.getProductById(id)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据关键字获取商品
const getProductsByKeyWords = async (ctx) => {
  const keywords = ctx.request.query
  const data = await product.getProductsByKeyWords(keywords)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据商品类别获取商品
const getProductsByCategory = async (ctx) => {
  const typeid = ctx.params.typeid
  const data = await product.getProductsByCategory(typeid)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据id修改商品
const updateProduct = async (ctx) => {
  const id = ctx.params.id
  const data = ctx.request.body
  const result = await product.updateProduct(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据id修改商品状态
const updateStatus = async (ctx) => {
  const id = ctx.params.id
  const data = ctx.request.body
  const result = await product.updateStatus(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据id修改商品库存
const updateInventory = async (ctx) => {
  const id = ctx.params.id
  const data = ctx.request.body
  const result = await product.updateInventory(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 获取某类商品销售量
const getSaleByCategory = async (ctx) =>{
  const id = ctx.params.id
  const data = await product.getSaleByCategory(id)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 获取最新商品
const getNewProducts = async (ctx) => {
  const data = await product.getNewProducts()
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据筛选条件获取商品
const getProductsByFilter = async (ctx) => {
  const query = ctx.request.query
  const data = await product.getProductsByFilter(query)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
module.exports = {
  getProducts,
  getStatusProducts,
  addProduct,
  getProductById,
  getProductsByKeyWords,
  updateProduct,
  updateStatus,
  getProductsByCategory,
  getHotProducts,
  updateInventory,
  getSaleByCategory,
  getNewProducts,
  getProductsByFilter
}